﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.SystemCore.DTO.RoleMenu;
using AnQinFrameWorkAPI.Entity.SystemCore.Entity;
using AnQinFrameWorkAPI.Entity.SystemCore.View.RoleMenu;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.SystemCore;

/// <summary>
///     角色菜单业务层
/// </summary>
public class RoleMenuService
{
    #region 获取角色菜单列表

    public async Task<RequestResponseModel> GetRoleMenuList(RoleMenuSearchDTO roleMenuSearchDTO)
    {
        var roleId = Guid.Parse(roleMenuSearchDTO.RoleId);

        var existingRoleMenuIds = await _context.RoleMenu
            .Where(rm => rm.RoleId == roleId)
            .Select(rm => rm.MenuId)
            .ToListAsync();

        var addRoleMenuList = await _context.Menu
            .Where(m => !existingRoleMenuIds.Contains(m.Id))
            .Select(m => new RoleMenuModel
            {
                Id = Guid.NewGuid(),
                RoleId = roleId,
                MenuId = m.Id,
                IsEnable = false
            })
            .ToListAsync();

        if (addRoleMenuList.Count > 0)
        {
            _context.RoleMenu.AddRange(addRoleMenuList);
            await _context.SaveChangesAsync();
        }

        var query = _context.RoleMenu
            .ProjectTo<RoleMenuView>(_mapper.ConfigurationProvider)
            .AsNoTracking().Where(a => a.RoleId.ToString() == roleMenuSearchDTO.RoleId)
            .WhereIF(a => a.MenuName.Contains(roleMenuSearchDTO.MenuName), !string.IsNullOrEmpty(roleMenuSearchDTO.MenuName));

        //返回列表
        var roleMenuList = await query
            .Page(roleMenuSearchDTO.PageIndex, roleMenuSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<RoleMenuView>(roleMenuList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 启用角色菜单

    public async Task<RequestResponseModel> EnableMultiRoleMenu(RoleMenuUpdateDTO roleMenuUpdateDto)
    {
        //查询角色菜单
        var query = await _context.RoleMenu
            .Where(a => a.RoleId.ToString() == roleMenuUpdateDto.RoleId)
            .Where(a => roleMenuUpdateDto.MenuId.Contains(a.MenuId.ToString()))
            .ToListAsync();

        if (query.Count != roleMenuUpdateDto.MenuId.Count)
            return RequestResponse.ErrorRequest("角色Id无效或者角色菜单列表中有菜单ID无效");

        //启用
        foreach (var item in query) item.IsEnable = true;

        _context.RoleMenu.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(roleMenuUpdateDto.MenuId, "启用成功");
    }

    #endregion

    #region 禁用角色菜单

    public async Task<RequestResponseModel> DisableMultiRoleMenu(RoleMenuUpdateDTO roleMenuUpdateDto)
    {
        //查询角色菜单
        var query = await _context.RoleMenu
            .Where(a => a.RoleId.ToString() == roleMenuUpdateDto.RoleId)
            .Where(a => roleMenuUpdateDto.MenuId.Contains(a.MenuId.ToString()))
            .ToListAsync();

        if (query.Count != roleMenuUpdateDto.MenuId.Count)
            return RequestResponse.ErrorRequest("角色Id无效或者角色菜单列表中有菜单ID无效");

        //禁用
        foreach (var item in query) item.IsEnable = false;

        _context.RoleMenu.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(roleMenuUpdateDto.MenuId, "禁用成功");
    }

    #endregion

    #region 初始化

    //数据库
    private readonly DatabaseContext _context;

    //autoMapper
    private readonly IMapper _mapper;

    //构造注入
    public RoleMenuService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #endregion
}